package com.pcbly.web.action.system;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pcbly.web.action.BaseAction;
import com.pcbly.web.bean.table.ColumnBean;
import com.pcbly.web.bean.table.TableBean;
import com.pcbly.web.business.table.ColumnService;
import com.pcbly.web.business.table.TableService;
import com.pcbly.web.components.utils.property.GlobalReadProperties;

public class ColumnAction extends BaseAction<ColumnBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1967833860670847788L;

	private ColumnService columnService;

	private TableService tableService;

	private ColumnBean columnBean;

	private List<ColumnBean> columnBeans;

	private String tableName;

	private String tableId;

	private String ids;

	public void setTableService(TableService tableService) {
		this.tableService = tableService;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String index() throws Exception {
		long start = System.currentTimeMillis();
		if (GlobalReadProperties.getStringToProperty("column.lockColimn")
				.indexOf("0") >= 0) {
			columnBeans = columnService.findColumnBeansFromDatabase(tableName);
			List<ColumnBean> list = null;
			if (log.isDebugEnabled()) {
				log.debug("index login.............");
				log.debug(tableId);
				System.out.println("index login.............");
				System.out.println(tableId);
			}
			if (!(tableId == null || "".equals(tableId))) {

				list = columnService.getColumnBeansByTableId(tableId);
			}
			System.out.println("index login 1.............");
			List<ColumnBean> saveList = new ArrayList<ColumnBean>();
			for (ColumnBean childBean : columnBeans) {
				boolean iflag = false;
				if (!list.isEmpty()) {
					for (ColumnBean bean : list) {
						if (bean.getColumnName().equals(
								childBean.getColumnName())) {
							iflag = true;
							break;
						}
					}
				}
				// log.debug(childBean.getColumnName()+""+iflag);
				// System.out.println(childBean.getColumnName()+""+iflag);
				if (!iflag) {
					TableBean tableBean = new TableBean();
					tableBean.setId(tableId);
					childBean.setIflag("0");
					childBean.setTableBean(tableBean);
					saveList.add(childBean);
				}
			}
			System.out.println("index login 2.............");
			if (!saveList.isEmpty())
				columnService.saveColumnBeans(saveList);
			System.out.println("index login 3.............");
		}
		if (log.isDebugEnabled()) {
			log.debug("insert end............."); 
			System.out.println("insert end............."); 
		}
		TableBean tableBean = tableService.getTableBeanById(tableId);
		String returnTabelName = tableService
				.getTableBeanFromDataBase(tableBean.getTableNameExtention());
		if (returnTabelName == null) {
			columnService.createTableExtention(tableBean
					.getTableNameExtention());
		}
		columnBeans = columnService.getColumnBeansByTableId(tableId);
		HttpServletResponse response = getResponse();
		HttpServletRequest request = getRequest();
		String WEBPATH = "http://" + request.getHeader("Host")
				+ request.getContextPath();
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml  version=\"1.0\" encoding=\"UTF-8\"?><root>");
		for (ColumnBean columnBean : columnBeans) {
			sb.append("<tree text=\"");
			sb
					.append(htmEncode(columnBean.getColumnCName() == null ? columnBean
							.getColumnName()
							: columnBean.getColumnName() + "("
									+ columnBean.getColumnCName() + ")"));
			sb.append("\" ");
			sb.append(" action=\"javascript:showCategoryValue('");
			sb.append(columnBean.getId());
			sb.append("','");
			sb.append(tableId == null ? columnBean.getTableBean().getId()
					: tableId);
			sb.append("');\"  icon=\"");
			sb.append(WEBPATH);
			sb.append("/images/xloadtree/page_white_gear.gif\"/>");
		}
		sb.append("</root>");
		if (log.isDebugEnabled()) {
			log.debug(sb.toString()); 
			System.out.println(sb.toString()); 
		}
		response.setContentType("text/xml; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		try {
			response.getWriter().write(sb.toString());
			response.getWriter().flush();
			response.getWriter().close();
			start = System.currentTimeMillis() - start;
			if (log.isDebugEnabled()) {
				log.debug("time ms:::::::::" + start);
				System.out.println("time ms:::::::::" + start);
			}
			return super.index();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String dwrCheck(String name) throws Exception {
		if (name == null || "".equals(name.trim())) {
			return "false";
		}
		boolean iflag = columnService.checkName(name.toUpperCase());
		if (iflag) {
			return "true";
		} else {
			return "false";
		}
	}

	public List<ColumnBean> dwrIndex(String id) throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("dwrIndex login.............");
			log.debug(id);
		}
		columnBeans = columnService.getColumnBeansByTableId(id);
		return columnBeans;
	}

	public ColumnBean dwrView(String id, String tableId) throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("dwrView login.............");
			log.debug(tableId);
			log.debug(id);
		}
		return columnService.getColumnBeanById(id);
	}

	public String dwrDelete(String id) throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("dwrDelete login.............");
			log.debug(id);
		}
		try {
			columnService.deleteColumnBeansById(id);
			return super.dwrBaseDelete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}

	public String dwrSave(ColumnBean bean) throws Exception {
		try {
			bean.setColumnName(bean.getColumnName().toUpperCase());
			// if(log.isDebugEnabled()){
			// log.debug(bean.getId());
			// log.debug(bean.getColumnName());
			// log.debug(bean.getColumnCName());
			// log.debug(bean.getColumnStyle());
			// System.out.println(bean.getId());
			// System.out.println(bean.getColumnCName());
			// System.out.println(bean.getColumnStyle());
			// System.out.println(bean.getColumnName());
			// }
			if (bean.getId() == null || "".equals(bean.getId())) {
				bean.setIflag("1");
				columnService.saveColumnBean(bean);
			} else {
				columnService.updateColumnBean(bean);
			}
			return super.dwrBaseSave(bean.getId());
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return "error";
		}
	}

	public String delete() throws Exception {
		columnService.deleteColumnBeansById(getId());
		saveMessage(getText("entity.deleted"));
		return super.delete();
	}

	public String deletes() throws Exception {
		columnService.deleteColumnBeansById(ids);
		saveMessage(getText("entity.deleted"));
		return super.delete();
	}

	public String save() throws Exception {
		String message;
		if (columnBean.getId() == null || "".equals(columnBean.getId())) {
			columnService.saveColumnBean(columnBean);
			message = getText("entity.saved");
		} else {
			columnService.updateColumnBean(columnBean);
			message = getText("entity.updated");
		}

		return message;
	}

	public String view() throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("dwrView login.............");
			log.debug(tableId);
			log.debug(getId());
		}
		columnBean = columnService.getColumnBeanById(getId());
		return super.view();
	}

	public ColumnBean getColumnBean() {
		return columnBean;
	}

	public void setColumnBean(ColumnBean columnBean) {
		this.columnBean = columnBean;
	}

	public List<ColumnBean> getColumnBeans() {
		return columnBeans;
	}

	public void setColumnBeans(List<ColumnBean> columnBeans) {
		this.columnBeans = columnBeans;
	}

	public void setColumnService(ColumnService columnService) {
		this.columnService = columnService;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getTableId() {
		return tableId;
	}

	public void setTableId(String tableId) {
		this.tableId = tableId;
	}

}
